﻿using System.Reflection;
using System.Windows;
using System.Windows.Media;
using System.Windows.Shapes;

namespace Com.Lancetinc.Evs.Reports.RadPdf;

internal class ShapeRenderer : UIElementRendererBase
{
    internal override bool Render(UIElement element, PdfRenderContext context)
    {
        var shape = element as Shape;
        if (shape == null)
        {
            return false;
        }

        var type = shape.GetType();
        var geometryProperty = type.GetProperty("DefiningGeometry",
            BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
        var geometry = (Geometry)geometryProperty.GetValue(shape, null);
        var pdfGeometry = PdfGeometryHelper.ConvertGeometry(geometry);
        if (pdfGeometry == null)
        {
            return false;
        }

        using (context.drawingSurface.SaveGraphicProperties())
        {
            SetFill(context, shape.Fill, shape.ActualWidth, shape.ActualHeight);
            SetStroke(context, shape.StrokeThickness, shape.Stroke, shape.ActualWidth, shape.ActualHeight,
                shape.StrokeDashArray);

            if (context.drawingSurface.GraphicProperties.IsFilled || context.drawingSurface.GraphicProperties.IsStroked)
            {
                context.drawingSurface.DrawPath(pdfGeometry);
            }

            return true;
        }
    }
}